package com.dongdongshop.operator.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.operator.pojo.TbBrand;
import com.dongdongshop.operator.service.TbBrandService;
import com.dongdongshop_util.Result;
import com.dongdongshop_util.ResultEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("brand")
public class TbBrandController {

    private final Logger logger = LoggerFactory.getLogger(TbBrandController.class);

    @Reference(check = false)
    private TbBrandService brandService;

    // 跳转到品牌管理页面
    @RequestMapping("toList")
    public String toBrand(){
        return "admin/brand";
    }

    //获取品牌管理数据
    @RequestMapping("getBrand")
    @ResponseBody
    public Result getBrand(){
        try {
            List<TbBrand> branfList = brandService.getBrand();
            return Result.success(ResultEnum.SUCCESS,branfList);
        } catch (Exception e){
            logger.error(String.valueOf(ResultEnum.FAIT),e);
            return Result.response(ResultEnum.FAIT);
        }
    }
    // 增加操作
    @RequestMapping("addBrand")
    @ResponseBody
    public Result addBrand(TbBrand brand){
        try {
            brandService.addBrand(brand);
            return Result.perfect(ResultEnum.SUCCESS);
        } catch (Exception e){
            logger.error(String.valueOf(ResultEnum.FAIT),e);
            return Result.response(ResultEnum.FAIT);
        }
    }
    //修改回显
    @RequestMapping("getBrandById")
    @ResponseBody
    public Result getBrandById(Long id){
        try {
            TbBrand brand = brandService.getBrandById(id);
            return Result.success(ResultEnum.SUCCESS,brand);
        } catch (Exception e){
            logger.error(String.valueOf(ResultEnum.FAIT),e);
            return Result.response(ResultEnum.FAIT);
        }
    }
    //修改数据
    @RequestMapping("updateBrandById")
    @ResponseBody
    public Result updateBrandById(TbBrand brand){
        try {
            brandService.updateBrandById(brand);
            return Result.perfect(ResultEnum.SUCCESS);
        } catch (Exception e){
            logger.error(String.valueOf(ResultEnum.FAIT),e);
            return Result.response(ResultEnum.FAIT);
        }
    }
    //删除数据
    @RequestMapping("deleteBrandByIds")
    @ResponseBody
    public Result deleteBrandByIds(@RequestParam("ids[]")Long[] ids){
        try {
            brandService.deleteBrandByIds(ids);
            return Result.perfect(ResultEnum.SUCCESS);
        } catch (Exception e){
            logger.error(String.valueOf(ResultEnum.FAIT),e);
            return Result.response(ResultEnum.FAIT);
        }

    }
}
